package com.cmccgz.baselibrary.utils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.bilibili.boxing.Boxing;
import com.bilibili.boxing.model.config.BoxingConfig;
import com.bilibili.boxing.model.config.BoxingCropOption;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.bilibili.boxing.model.entity.impl.ImageMedia;
import com.bilibili.boxing.utils.BoxingFileHelper;
import com.bilibili.boxing.utils.ImageCompressor;
import com.bilibili.boxing_impl.ui.BoxingActivity;
import com.cmccgz.baselibrary.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static android.app.Activity.RESULT_OK;


public class BoxingUtils {
    private static final int REQUEST_CODE = 1024;

    public static void selectMuliImage(Context mContext, int count,ArrayList<BaseMedia>selectMedias) {
        BoxingConfig config = new BoxingConfig(BoxingConfig.Mode.MULTI_IMG)
                .needCamera(R.drawable.ic_boxing_camera_white)
                .needGif()
                .withMaxCount(count);
        if (selectMedias==null) {
            selectMedias=new ArrayList<>();
        }
        Boxing.of(config).withIntent(mContext, BoxingActivity.class,selectMedias).start(((Activity) mContext), REQUEST_CODE);
    }

    public static void selectMuliImage(Context mContext, int count) {
        selectMuliImage(mContext,count,null);
    }

    public static void selectSingleImage(Context mContext, boolean isCrop) {
        BoxingConfig config = new BoxingConfig(BoxingConfig.Mode.SINGLE_IMG)
                .needCamera(R.drawable.ic_boxing_camera_white)
                .needGif();

        if (isCrop) {
            String cachePath = BoxingFileHelper.getCacheDir(mContext);
            if (TextUtils.isEmpty(cachePath)) {
                ToastUtils.show(mContext,R.string.boxing_storage_deny);
                return;
            }
            Uri destUri = new Uri.Builder()
                    .scheme("file")
                    .appendPath(cachePath)
                    .appendPath(String.format(Locale.US, "%s.png", System.currentTimeMillis()))
                    .build();
            config = config.withCropOption(new BoxingCropOption(destUri));
        }

        Boxing.of(config).withIntent(mContext, BoxingActivity.class).start(((Activity) mContext), REQUEST_CODE);
    }

    /**
     * 获取url的结果(可压缩)
     * @param requestCode
     * @param resultCode
     * @param data
     * @param mContext
     * @param maxSize 结果最大大小，当图片大小超过maxSize则压缩
     * @return
     */
    public static List<String> getUrlResult(int requestCode, int resultCode, Intent data, Context mContext, int maxSize) {
        List<String> result = new ArrayList<>();
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            ArrayList<BaseMedia> medias = Boxing.getResult(data);
            for (int i = 0; medias != null && i < medias.size(); i++) {
                ImageMedia media = ((ImageMedia) medias.get(i));
                String path = media.getPath();
                if (maxSize > 0 && media.compress(new ImageCompressor(mContext), maxSize)) {
                    media.removeExif();
                    path = media.getCompressPath();
                }
                result.add(path);
            }
        }
        return result;
    }

    /**
     *  获取url的结果（不压缩）
     * @param requestCode
     * @param resultCode
     * @param data
     * @param mContext
     * @return
     */
    public static List<String> getUrlResult(int requestCode, int resultCode, Intent data, Context mContext) {
        return getUrlResult(requestCode,resultCode,data,mContext,0);
    }

    /**
     * 获取ImageMedia的结果（可压缩）
     * @param requestCode
     * @param resultCode
     * @param data
     * @param mContext
     * @param maxSize
     * @return 结果最大大小，当图片大小超过maxSize则压缩
     */
    public static List<ImageMedia> getMediaResult(int requestCode, int resultCode, Intent data, Context mContext, int maxSize) {
        List<ImageMedia> result = new ArrayList<>();
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            ArrayList<BaseMedia> medias = Boxing.getResult(data);
            for (int i = 0; medias != null && i < medias.size(); i++) {
                ImageMedia media = ((ImageMedia) medias.get(i));
                if (maxSize > 0 && media.compress(new ImageCompressor(mContext), maxSize)) {
                    media.removeExif();
                }
                result.add(media);
            }
        }
        return result;
    }

    /**
     * 获取ImageMedia的结果
     * @param requestCode
     * @param resultCode
     * @param data
     * @param mContext
     * @return
     */
    public static List<ImageMedia> getMediaResult(int requestCode, int resultCode, Intent data, Context mContext) {
      return getMediaResult(requestCode,resultCode,data,mContext,0);
    }

    public static List<ImageMedia>compress(Context context,List<ImageMedia>imgs,int maxSize){
        if (imgs==null)
            return null;
        ImageCompressor imageCompressor = new ImageCompressor(context);
        for (ImageMedia img : imgs) {
            img.compress(imageCompressor,maxSize);
        }
        return imgs;
    }

    public static List<String>compress(Context context,ArrayList<String>imgUrls,int maxSize){
        if (imgUrls==null)
            return null;
        List<String>result=new ArrayList<>();
        ImageCompressor imageCompressor = new ImageCompressor(context);
        for (int i = 0; i < imgUrls.size(); i++) {
            String imgUrl = imgUrls.get(i);
            ImageMedia imageMedia = new ImageMedia(i + "", imgUrl);
            imageMedia.setSize(new File(imgUrl).length()+"");
            result.add(imageMedia.compress(imageCompressor,maxSize)?imageMedia.getCompressPath():imageMedia.getPath());
        }

        return result;
    }

    public static ImageMedia compress(Context context,ImageMedia img,int maxSize){
        if (img==null) {
            return null;
        }
        img.compress(new ImageCompressor(context),maxSize);
        return img;
    }
    public static String compress(Context context,String imgUrl,int maxSize){
        if (imgUrl==null) {
            return null;
        }
        ImageMedia imageMedia = new ImageMedia("0", imgUrl);
        imageMedia.setSize(new File(imgUrl).length()+"");
        return imageMedia.compress(new ImageCompressor(context),maxSize)?imageMedia.getCompressPath():imageMedia.getPath();
    }
}
